/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package listenerthread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerListenerThread implements Runnable {

    private Socket client;
    private String clientName;
    private int clientId;
    private int opponentId;
    private ServerListenerThread opponent;
    private ServerListenerThread requestOpponent;
    private BufferedReader in;
    private PrintWriter out;
    private int requestedOpponentId;
    private ServerListenerThreadIn threadIn;
    private ServerListenerThreadOut threadOut;

    //Constructor
    public ServerListenerThread(Socket client, int clientId) {
        this.clientId = clientId;
        this.client = client;
    }

    public void run() {
        try {

            in = new BufferedReader(
                    new InputStreamReader(client.getInputStream()));
            out = new PrintWriter(
                    client.getOutputStream(), true);

            System.out.println("LOADING ThreadIn...");
            threadIn = new ServerListenerThreadIn(in, this);
            Thread threadInThread = new Thread(threadIn);

            System.out.println("LOADING ThreadOut...");
            threadOut = new ServerListenerThreadOut(out, this);
            Thread threadOutThread = new Thread(threadOut);
            threadOutThread.start();


            threadInThread.start();
            System.out.println("DONE");



        } catch (IOException e) {
            System.out.println("in or out failed");
            System.exit(-1);
        }

    }

    protected void finalize() {
        //Objects created in run method are finalized when
        //program terminates and thread exits
        try {

            client.close();

        } catch (IOException e) {
            System.out.println("Could not close socket");
            System.exit(-1);
        }
    }

    public int getClientId() {
        return clientId;
    }

    public void setClientId(int clientId) {
        this.clientId = clientId;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
        System.out.println("CONNECTED:" + client.getInetAddress().getHostName() + ", ID: " + clientId
                + ", NAME: " + clientName);
    }

    public int getOpponentId() {
        return opponentId;
    }

    public void setOpponentId(int opponentId) {
        this.opponentId = opponentId;
    }

    public ServerListenerThread getRequestOpponent() {
        return requestOpponent;
    }

    public void setRequestOpponent(ServerListenerThread requestOpponent) {
        this.requestOpponent = requestOpponent;
    }

    public ServerListenerThread getOpponent() {
        return opponent;
    }

    public void setOpponent(ServerListenerThread opponent) {
        this.opponent = opponent;
        System.out.println("Opponent Id: " + opponentId + ", Opponent Name: " + opponent.getClientName());
    }

    public int getRequestedOpponentId() {
        return requestedOpponentId;
    }

    public void setRequestedOpponentId(int requestedOpponentId) {
        this.requestedOpponentId = requestedOpponentId;
    }

    public ServerListenerThreadIn getThreadIn() {
        return threadIn;
    }

    public ServerListenerThreadOut getThreadOut() {

        return threadOut;
    }

    public Socket getClient() {
        return client;
    }
}
